<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/1/13
 * Time: 9:29
 */

namespace app\controller\admin;

use app\model\admin\Admin;
use think\facade\Cookie;
use think\facade\View;

class Base
{
    protected $user = null;
    protected $page = 1;
    protected $size = 15;
    protected $start_time = '';
    protected $end_time = '';
    public function __construct()
    {
        global $user;
        $userInfo = Cookie::get('user');
        if (empty($userInfo)){
            header("Location: /admin/login" );
            die;
        }
        $userInfo = json_decode($userInfo);
        $user = Admin::where('username',$userInfo->username)->find();
        if (empty($user)) {
            header("Location: /admin/login" );
            die;
        }
        $this->user = $user;
        $this->page = input('page',1);
        $this->size = input('size',10);
        $this->start_time = input('start_time');
        $this->end_time = input('end_time');
        View::assign('userInfo',$user);
    }

    /**
     * 返回视图
     * @param string $view
     * @param array $data
     * @return string
     */
    protected function view($view = 'index', array $data = []){
        View::assign($data);
        return View::fetch($view);
    }

    protected function msg($data = [],string $msg = '成功',int $code = 200,int $http_code = 200){
        return json(['code' => $code,'msg' => $msg,'data' => $data],$http_code);
    }

    protected function fail($msg = 'fail',$code = 400,$http_code = 200){
        return json(['code' => $code,'msg' => $msg,'data' => (object)[]],$http_code);
    }
}